<?php

namespace App\Admin\Controllers;

use App\Admin\Metrics\Examples;
use App\Http\Controllers\Controller;
use Dcat\Admin\Http\Controllers\Dashboard;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Grid;
use App\Models\Project;
use App\Models\ProjectUser;
use Dcat\Admin\Admin;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index(Content $content)
    {
        $a = Admin::user()->roles->toArray();
        if($a[0]['id']==9){ //超级管理员可以删除
            return redirect('/admin/project/ProjectLocationView/8');
        }
        return redirect('/admin/new-index');
        // return $content->full()->body($this->grid());
        //超级管理员，可全部查看
        /*if (Admin::user()->isAdministrator() || Admin::user()->isRole('superadmin')) {
            $model = new Project();
            return $content
                ->header('项目列表')
                ->description('选择项目查看详情')
                ->body($this->grid($model));
        } else {
            $allowProjects = ProjectUser::where('user_id', Admin::user()->id)->pluck('projectId')->toArray();
            $model = Project::whereIn('projectId', $allowProjects);
            $projectId = session('now_project');
            if ($projectId) {
                return $content
                ->header('项目列表')
                ->description('选择项目查看详情')
                ->body($this->grid($model));
            } else {
                return $content->selfFull()->body($this->grid($model));
            }
        }*/
    }

    protected function grid($projectModel)
    {
        return Grid::make($projectModel, function (Grid $grid) {
            $grid->column('projectname','项目');
            $grid->column('projectId', '操作')->display(function () {
                return '<a href="/admin/projectDesc/'.$this->projectId.'"><span style="color:green">详细档案</span></a>';
            });

            $grid->disableRowSelector();
            $grid->disableToolbar();
            $grid->disableActions();
        });
    }

    /**
     * [setAutoRefresh 设置轮询频率]
     * @param Request $request [description]
     */
    public function setAutoRefresh(Request $request){
        $param = $request->all();
        $key = $param['key'];
        $r = session()->put($key, $param['value']);
        return response()->json(['code'=>1, 'data' => [], 'msg' => 'success']);
    }

    public function setCompareDate(Request $request){
        $param = $request->all();
        $r = session()->put('compare_date', $param['date']);
        return response()->json(['code'=>1, 'data' => [], 'msg' => 'success']);
    }
}
